#include "client_puts_file.h"


//TCP 是一个字节流协议，消息没有边界

int transferfile(int peerfd,const char *filename)
{
    //char sync[100]={0};
    //recv(peerfd, sync, 7, MSG_WAITALL);
    //1.读取本地文件
    int fd=open(filename,O_RDWR);
    if(fd==-1)
    {
        printf("read local file:%s is failed\n",filename);
        size_t fail=0;
        send(peerfd,&fail,8,0);
        return -1;
    }
    //文件内容
    //3.1 获取文件长度
    struct stat st;
    fstat(fd,&st);
    printf("file length:%ld\n",st.st_size);
    send(peerfd,&st.st_size,8,0);//发送文件大小
    //进行映射mmap
    char *pbuf=(char*)mmap(NULL,st.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    printf("send peerfd:%d\n",peerfd);
    int ret=send(peerfd,pbuf,st.st_size,MSG_WAITALL);
    printf(">>>send ret:%d\n",ret);
    munmap(pbuf,st.st_size);
    close(fd);
    return 0;
}


